/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ihm;

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.*;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import com.ihm.actions.*;
import com.dao.*;

public class FenetreConsultation extends JPanel 
{

    public static JPanel getPanelmiddle() {
		return panelmiddle;
	}

	public static void setPanelmiddle(JPanel panelmiddle) {
		FenetreConsultation.panelmiddle = panelmiddle;
	}

	public static JPanel getPanelbas() {
		return panelbas;
	}

	public static void setPanelbas(JPanel panelbas) {
		FenetreConsultation.panelbas = panelbas;
	}

	public static ArrayList<JPanel> getListpanel() {
		return listpanel;
	}

	public static void setListpanel(ArrayList<JPanel> listpanel) {
		FenetreConsultation.listpanel = listpanel;
	}

	public static CartePreparedRequete getCarteps() {
		return carteps;
	}

	public static void setCarteps(CartePreparedRequete carteps) {
		FenetreConsultation.carteps = carteps;
	}

	private JPanel paneltop;
    private JPanel paneltrier;
    private static JPanel panelmiddle;
    private static JPanel panelbas;
    private JPanel panelbasmiddle;
    private JPanel panelbasdroite;
    private JPanel panelbasgauche;
    private JPanel panelbasmiddlehaut;
    private JPanel panelbasmiddlebas;
    private JPanel mainpanel ;
    private JLabel labelcarte;
    private JLabel ListeLieu;
    private JLabel lieu;
    private JLabel trier;
    private JComboBox triercombo;
    private JLabel labelrecherche;
    private JLabel nomdutitre;
    private JLabel nomdudescription;
    private static JTextField texttitre;
    private static JTextField textdescription;
    private static ArrayList<String> listtitre ;
    private static ArrayList<String> stringenregistre ;
    private static ArrayList<JPanel> listenregistrepanel ;
    private static ArrayList<String> listDate ;
    private static ArrayList<String> dateenregistre ;
     public static JTextField getTexttitre() {
		return texttitre;
	}

	public static void setTexttitre(JTextField texttitre) {
		FenetreConsultation.texttitre = texttitre;
	}

	public static JTextField getTextdescription() {
		return textdescription;
	}

	public static void setTextdescription(JTextField textdescription) {
		FenetreConsultation.textdescription = textdescription;
	}

	private JButton buttonrecherche;
    private JButton buttonannuler ;
    private static ArrayList<JPanel> listpanel;
    private static CartePreparedRequete carteps;
    private ArrayList<JPanel> listfiltrepanel ;
    
    public FenetreConsultation()
    {
	this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
	createpanel();
	createcomponent();
	addcomponent();
	event();
	this.add(mainpanel);
        carteps = new CartePreparedRequete();
        try 
        {
            carteps.afficher();
        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
        listfiltrepanel = new ArrayList<>();
        listpanel = new ArrayList<>();
        listtitre = new ArrayList<>();
        stringenregistre = new ArrayList<>();
        listenregistrepanel = new ArrayList<>() ;
        listDate = new ArrayList<>();
        dateenregistre = new ArrayList<>();
	    createlist();
      }
      
      public void createpanel()
      {
            paneltop = new JPanel();
	    paneltop.setMaximumSize(new Dimension(700,200));
	    paneltop.setSize(new Dimension(700,200));
	    paneltop.setLayout(new BoxLayout(paneltop,BoxLayout.X_AXIS));
            //paneltop.setBackground(Color.white);
            
	    paneltrier = new JPanel();
	    paneltrier.setMaximumSize(new Dimension(700,100));
	    paneltrier.setSize(new Dimension(700,100));
	    paneltrier.setLayout(new BoxLayout(paneltrier,BoxLayout.X_AXIS));
            //paneltrier.setBackground(Color.white);
            
	    panelmiddle = new JPanel();
	    panelmiddle.setMaximumSize(new Dimension(700,500));
	    panelmiddle.setSize(700, 500);
	    panelmiddle.setLayout(new BoxLayout(panelmiddle,BoxLayout.Y_AXIS));
            
	    panelbas = new JPanel();
	    panelbas.setMaximumSize(new Dimension(700,200));
	    panelbas.setSize(new Dimension(700,200));
	    panelbas.setLayout(new BoxLayout(panelbas,BoxLayout.X_AXIS));
            
	    panelbasgauche = new JPanel();
	    panelbasgauche.setMaximumSize(new Dimension(300,200));
	    panelbasgauche.setSize(new Dimension(300,200));
            
	    panelbasmiddle = new JPanel();
	    panelbasmiddle.setMaximumSize(new Dimension(500,200));
	    panelbasmiddle.setSize(new Dimension(500,200));
	    panelbasmiddle.setLayout(new BoxLayout(panelbasmiddle,BoxLayout.Y_AXIS));
            
	    panelbasdroite = new JPanel();
	    panelbasdroite.setMaximumSize(new Dimension(300,200));
	    panelbasdroite.setSize(new Dimension(300,200));
            
	    panelbasmiddlehaut = new JPanel();
	    panelbasmiddlehaut.setLayout(new BoxLayout(panelbasmiddlehaut,BoxLayout.X_AXIS));
            
	    panelbasmiddlebas = new JPanel();
	    panelbasmiddlebas.setLayout(new BoxLayout(panelbasmiddlebas,BoxLayout.X_AXIS));
	    
	    mainpanel = new JPanel();
	    mainpanel.setLayout(new BoxLayout(mainpanel,BoxLayout.Y_AXIS));
	 }
      
	 public void createcomponent()
	 {
                labelcarte = new JLabel("Consultation des cartes");
                labelcarte.setFont(new Font("italique",Font.BOLD,20));
                labelcarte.setForeground(Color.gray);
                ListeLieu = new JLabel();
                ListeLieu.setFont(new Font("italique",Font.BOLD,20));
                lieu = new JLabel("LISTE");
                lieu.setFont(new Font("italique",Font.BOLD,16));
                trier = new JLabel("Trier par : ");
                triercombo = new JComboBox();
                triercombo.setMaximumSize(new Dimension(80,20));
                triercombo.setSize(new Dimension(80,20));
                triercombo.addItem("A-Z");
                triercombo.addItem("Z-A");
                triercombo.setSelectedIndex(0);
                labelrecherche = new JLabel("RECHERCHER UN LIEU");
                labelrecherche.setFont(new Font("italique",Font.BOLD,15));
                nomdutitre = new JLabel("Titre : ");
                texttitre = new JTextField();
                texttitre.setMaximumSize(new Dimension(220,20));
                texttitre.setSize(new Dimension(220,20));
                nomdudescription =new JLabel("Description : ");
                textdescription = new JTextField();
                buttonrecherche = new JButton("Chercher");
                buttonannuler = new JButton("Annuler");
	  }
         
	  public void addcomponent()
          {   
                paneltop.add(Box.createHorizontalStrut(20));
                paneltop.add(labelcarte);
                paneltop.add(Box.createHorizontalStrut(40));
                paneltop.add(ListeLieu);
                paneltop.add(Box.createHorizontalGlue());
                paneltrier.add(Box.createHorizontalStrut(20));
                paneltrier.add(lieu);
                paneltrier.add(Box.createHorizontalGlue());
                paneltrier.add(trier);
                paneltrier.add(Box.createHorizontalStrut(20));
                paneltrier.add(triercombo);
                paneltrier.add(Box.createHorizontalStrut(20));
                
                panelbas.add(panelbasgauche);
                panelbas.add(panelbasmiddle);
                panelbas.add(panelbasdroite);
                
                panelbasgauche.add(Box.createVerticalStrut(20));
                panelbasgauche.add(labelrecherche);
                
                panelbasmiddlehaut.add(nomdutitre);
                panelbasmiddlehaut.add(texttitre);
                panelbasmiddlebas.add(nomdudescription);
                panelbasmiddlebas.add(textdescription);
                
                panelbasmiddle.add(panelbasmiddlehaut);
                panelbasmiddle.add(panelbasmiddlebas);
                panelbasdroite.add(Box.createHorizontalGlue());
                panelbasdroite.add(buttonrecherche);
                panelbasdroite.add(Box.createHorizontalStrut(40));
                panelbasdroite.add(buttonannuler);
                
                mainpanel.add(paneltop);
            	mainpanel.add(paneltrier);
            	mainpanel.add(panelmiddle);
            	mainpanel.add(Box.createVerticalGlue());
            	mainpanel.add(panelbas);
	   }
	       public void createfiltrelist()
	       {
	    	   if(carteps.getDatefiltrelist().size()>0)
	    	   {
	    		   panelmiddle.removeAll();
	    		   listfiltrepanel.clear();
	    		   for(int i= 0 ;i<carteps.getDatefiltrelist().size();i++)
	    		   {
	    			   JPanel mainpanel = new JPanel();
	    			   JPanel panelhaut = new JPanel();
	    			   panelhaut.setLayout(new BoxLayout(panelhaut,BoxLayout.X_AXIS));
	    			   JPanel panelbas = new JPanel();
	    			   mainpanel.setLayout(new BoxLayout(mainpanel,BoxLayout.Y_AXIS));
	    			   mainpanel.setMaximumSize(new Dimension(700,300));
	    			   mainpanel.setSize(700,300);
	    			   JLabel labeldescription = new JLabel("Description : ");
	    			   JTextArea description = new JTextArea();
	    			   description.setEditable(false);
	    			   description.setText(carteps.getDescriptionfiltre().get(i).toString());
	            		 JLabel labeltitre = new JLabel("Titre : "+carteps.getTitrefiltre().get(i).toString());
	            		 JLabel labeldate = new JLabel("Date : "+carteps.getDatefiltrelist().get(i).toString());	
	            		 JButton buttonconsulter = new JButton("Consulter");
	            		 panelhaut.add(labeltitre);
	            		 panelhaut.add(Box.createHorizontalStrut(20));
	            		 panelhaut.add(labeldate);
	            		 panelhaut.add(Box.createHorizontalStrut(20));
	            		 panelhaut.add(buttonconsulter);
	            		 panelbas.add(labeldescription);
	            		 panelbas.add(Box.createHorizontalStrut(150));
	            		 panelbas.add(description);
	            		 panelbas.add(Box.createHorizontalStrut(150));
	            		 mainpanel.add(panelhaut);
	            		 mainpanel.add(panelbas);
	            		 mainpanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
	            		 listfiltrepanel.add(mainpanel);
	    		   }
	    	   
	            		 for(int i=0;i<listfiltrepanel.size();i++)
	                	 {
	                		 panelmiddle.add(listfiltrepanel.get(i));
	                	 }
	            		 }
	                	 else
	                	 {
	                		 JOptionPane.showMessageDialog(null,"Il n'y a aucune resultat correspondant");
	                	 }
	    			   
	    		   
	    		   
	    	   
	       }
           public void event()
           {   triercombo.addItemListener(new ItemListener(){

				@Override
				public void itemStateChanged(ItemEvent e) {
					// TODO Auto-generated method stub
                       if(e.getStateChange()==ItemEvent.SELECTED)
						{String index =(String)e.getItemSelectable().getSelectedObjects()[0];
						  mainpanel.removeAll();
						  panelmiddle.setMaximumSize(new Dimension(700,500));
			                 panelmiddle.setSize(new Dimension(700,500));
				        	 panelmiddle.setLayout(new BoxLayout(panelmiddle,BoxLayout.Y_AXIS));
						if(index=="A-Z")
						{   
							Collections.sort(listtitre,String.CASE_INSENSITIVE_ORDER);
                           for(int i=0;i<listtitre.size();i++)
                           { 
                           	for(int j=0;j<stringenregistre.size();j++)
                           	{
                           		if(stringenregistre.get(j)==listtitre.get(i))
                           		{
                           			listenregistrepanel.add(listpanel.get(j));
                           		}
                           	}
                           	}
                           for(int i=0;i<listenregistrepanel.size();i++)
                           {
                           	panelmiddle.add(listenregistrepanel.get(i));
                           }
                           }
						else if(index=="Z-A")
						{
								Collections.sort(listtitre,String.CASE_INSENSITIVE_ORDER);
								Collections.reverse(listtitre);
							
	                           for(int i=0;i<listtitre.size();i++)
	                           { 
	                           	for(int j=0;j<stringenregistre.size();j++)
	                           	{
	                           		if(stringenregistre.get(j)==listtitre.get(i))
	                           		{
	                           			listenregistrepanel.add(listpanel.get(j));
	                           		}
	                           	}
	                           	}
	                           for(int i=0;i<listenregistrepanel.size();i++)
	                           {
	                           	panelmiddle.add(listenregistrepanel.get(i));
	                           }
						}
						mainpanel.add(paneltop);
		            	mainpanel.add(paneltrier);
		            	mainpanel.add(panelmiddle);
		            	mainpanel.add(Box.createVerticalGlue());
		            	mainpanel.add(panelbas);
						}			
					}}

       		 
				);
        	   buttonannuler.addMouseListener(new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				mainpanel.removeAll();
				listpanel.clear();
				panelmiddle.removeAll();
				panelmiddle.setLayout(new BoxLayout(panelmiddle,BoxLayout.Y_AXIS));
				panelmiddle.setMaximumSize(new Dimension(700,500));
				panelmiddle.setSize(700,500);
				createlist();
			 mainpanel.add(paneltop);
        	 mainpanel.add(paneltrier);
        	 mainpanel.add(panelmiddle);
        	 mainpanel.add(Box.createVerticalGlue());
        	 mainpanel.add(panelbas);
        	 texttitre.setText(null);
        	 textdescription.setText(null);
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
        	   
           });
        	   buttonrecherche.addMouseListener(new MouseListener(){

				@Override
				public void mouseClicked(MouseEvent arg0) {
					// TODO Auto-generated method stub
					if(!texttitre.getText().toString().isEmpty()||!textdescription.getText().toString().isEmpty())
					{
						try {
							carteps.afficherfilterconsultation();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						mainpanel.removeAll();
						 mainpanel.removeAll();
						 panelmiddle.setMaximumSize(new Dimension(700,500));
		                 panelmiddle.setSize(new Dimension(700,500));
						 panelmiddle.setLayout(new BoxLayout(panelmiddle,BoxLayout.Y_AXIS));
						 createfiltrelist();
						 mainpanel.add(paneltop);
			        	 mainpanel.add(paneltrier);
			        	 mainpanel.add(panelmiddle);
			        	 mainpanel.add(Box.createVerticalGlue());
			        	 mainpanel.add(panelbas);
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Veuillez-vous remplir au moins un champs");
					}
				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mousePressed(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseReleased(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}
        		   
        	   });
           }

           public static void createlist()
           {     listpanel.clear();
                     
                 for(int i=0;i<carteps.getDatelist().size();i++)
                 {
                         JPanel mainpanel = new JPanel();
                         JPanel panelhaut = new JPanel();
                         JPanel baspanel = new JPanel();
                         baspanel.setMaximumSize(new Dimension(700,300));
                         baspanel.setSize(700,300);
                         panelhaut.setLayout(new BoxLayout(panelhaut,BoxLayout.X_AXIS));
                         mainpanel.setLayout(new BoxLayout(mainpanel,BoxLayout.Y_AXIS));
                         mainpanel.setMaximumSize(new Dimension(700,400));
                         mainpanel.setSize(new Dimension(700,400));
                         JLabel labeltitre = new JLabel("Titre :"+ carteps.getTitre().get(i).toString());
                         JLabel labeldate = new JLabel(carteps.getDatelist().get(i).toString());
                         JLabel labelDescription = new JLabel("Description :");
                         JTextArea DescriptionArea = new JTextArea();
                         DescriptionArea.setEditable(false);
                         DescriptionArea.setMaximumSize(new Dimension(700,300));
                         DescriptionArea.setSize(700,300);
                         DescriptionArea.setText(carteps.getDescription().get(i).toString());
                         final String descriptiontext =carteps.getDescription().get(i).toString();
                         final String titretext =carteps.getTitre().get(i).toString();
                  	 final String id_carte = carteps.getIdlist().get(i).toString();
                         JButton buttonconsulter = new JButton(new VisualisationCarteAction("Consulter",id_carte));
                   
                         panelhaut.add(labeltitre);
                         panelhaut.add(Box.createHorizontalStrut(20));
                         panelhaut.add(labeldate);
                         panelhaut.add(Box.createHorizontalStrut(20));
                         panelhaut.add(buttonconsulter);
                         panelhaut.add(Box.createHorizontalStrut(150));
                         baspanel.add(labelDescription);
                         baspanel.add(Box.createHorizontalStrut(150));
                         baspanel.add(DescriptionArea);
                         baspanel.add(Box.createHorizontalStrut(150));
                         mainpanel.add(panelhaut);
                         mainpanel.add(baspanel);
                         mainpanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                         listtitre.add(carteps.getTitre().get(i).toString());
                		 stringenregistre.add(carteps.getTitre().get(i).toString());
                		 listDate.add(carteps.getDatelist().get(i).toString());
                		 dateenregistre.add(carteps.getDatelist().get(i).toString());
                         listpanel.add(mainpanel);
                }

                for(int i=0;i<listpanel.size();i++)
                {
                    panelmiddle.add(listpanel.get(i));
                }
         }
}

